/**   
 * @Title: ResourceService.java 
 * @Package com.betterjr.modules.workflow.snaker.core 
 * @Description: 流程实例操作类
 * @author licz
 * @date 2017年10月30日 下午3:03:48  
 */
package com.betterjr.modules.risk.loanmanage.service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.betterjr.common.selectkey.SerialGenerator;
import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.BetterDateUtils;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.mapper.pagehelper.Page;
import com.betterjr.modules.risk.loanmanage.dao.RepaymentDetailsMapper;
import com.betterjr.modules.risk.loanmanage.entity.RepaymentDetails;
import com.betterjr.modules.risk.loanmanage.util.BetterBeanUtils;

@Service
public class RepaymentDetailsService extends BaseService<RepaymentDetailsMapper, RepaymentDetails> {
    // -1没完成(默认)，0完成，1部分完成，2逾期 ，3手动完成
    public Page<RepaymentDetails> listRepaymentDetails(final RepaymentDetails repaymentDetails, final int pageNum,
            final int pageSize) {
        return this.selectByPage(repaymentDetails, pageNum, pageSize, true);
    }

    // 修改状态未完成的 变成 手动完成
    public void manuallyCompletedByLoanDataId(final Long loanDataId) {
        final RepaymentDetails repaymentDetails = new RepaymentDetails();
        repaymentDetails.setStatus("3");
        final Map<String, Object> map = new HashMap<>();
        map.put("loanDataId", loanDataId);
        map.put("status", new String[] { "-1", "1", "2" });
        this.updateByExampleSelective(repaymentDetails, map);
    }

    public RepaymentDetails saveRepaymentDetails(final Map<String, Object> param) {

        final RepaymentDetails repaymentDetails = new RepaymentDetails();
        BetterBeanUtils.copyProperties2Bean(repaymentDetails, param);
        BTAssert.notNull(repaymentDetails, "参数不允许为空！");
        repaymentDetails.setCreatorId(UserUtils.getUser().getId());
        repaymentDetails.setCreator(UserUtils.getUser().getName());
        repaymentDetails.setCreateDate(Timestamp.valueOf(BetterDateUtils.getDateTime()));
        repaymentDetails.setUpdateCreatorId(UserUtils.getUser().getId());
        repaymentDetails.setUpdateCreator(UserUtils.getUser().getName());
        repaymentDetails.setUpdateDate(Timestamp.valueOf(BetterDateUtils.getDateTime()));
        if (!ObjectUtils.isEmpty(repaymentDetails.getId())) {
            this.updateByPrimaryKeySelective(repaymentDetails);
        } else {
            final Long baseId = SerialGenerator.getLongValue(RepaymentDetails.selectKey);
            repaymentDetails.setId(baseId);
            this.insertSelective(repaymentDetails);
        }
        return repaymentDetails;
    }

}
